package com.yyi.springcloud.controller;
//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//

import com.github.pagehelper.PageInfo;
import com.yyi.springcloud.common.PageInfoResult;
import com.yyi.springcloud.entities.*;
import com.yyi.springcloud.service.CurveService;
import com.yyi.springcloud.utils.ExportExcelUtils_DataList_Mysql;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.awt.geom.Curve;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author: yyi
 * @date: 2019/7/16 9:01
 * @parameters:
 * @desc:曲线绘制
 * 1 HistoryData
 * 2 DataList
 * 3 CurveDisplayContent
 * 4 分两部分  1） 页面左侧（传感器列表） 2）页面的右侧 曲线（主体内容）
 **/
@RestController
@RequestMapping("/consumer/CurveInfoAPI")
@CrossOrigin
public class CurveController {
    @Autowired
    private CurveService curveService;
    //http://localhost/consumer/CurveInfoAPI/getSensorInfoByCondition?labcode=Haierrefrigerator20151214&primaryKey=2017-11-1611:17:211

    @CrossOrigin
    @RequestMapping(value="/getSensorInfoByCondition",method = RequestMethod.GET)
    //传感器列表
    public List<SensorInfoModel> getSensorInfoByCondition(String labcode,String primaryKey){
        return curveService.getSensorInfoByCondition(labcode,primaryKey);
    }
    //根据传感器编号和primarykey获取最新一条数据
    //shit
    @CrossOrigin
    @RequestMapping(value = "/getNewOneTestData", method = RequestMethod.GET)
    public List<TestDataDto> getNewOneTestData(String primaryKey, String sensorNo) {
       List<TestDataDto> list =  curveService.getNewOneTestData(primaryKey,sensorNo);
        System.out.println(list);
       return list;
    }
    //根据实验室编码查询所有的在测台位的名牌信息
    //http://localhost/consumer/CurveInfoAPI/getAllTestProdInfoValue?labcode=Haierrefrigerator20151214
    @CrossOrigin
    @RequestMapping(value = "/getAllTestProdInfoValue",method=RequestMethod.GET)
    public PageInfo<TestProdInfoValueModel> getAllTestProdInfoValue(HttpServletRequest request){
        String labcode = request.getParameter("labcode");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        return curveService.getAllTestProdInfoValue(labcode,pageSize,pageNum);
    }
    //设置坐标轴
    //http://localhost/consumer/CurveInfoAPI/getCoordonate?primaryKey=2017-11-1611:17:211
    @CrossOrigin
    @RequestMapping(value = "/getCoordonate",method = RequestMethod.GET)
    public List<CoordonateVo> getCoordonate(String primaryKey){
        return curveService.getCoordonate(primaryKey);
    }
    //根据x轴值获取y轴值(请求后台获取)
    //http://localhost/consumer/CurveInfoAPI/getYAxisValue?labcode=Haierrefrigerator20151214&primaryKey=2017-11-1611:17:211&sensorno=1&xAxisValue=1
    @CrossOrigin
    @RequestMapping(value = "/getYAxisValue",method = RequestMethod.GET)
    public String getYAxisValue(String labcode,String primaryKey,String sensorNo,String xAxisValue){
        return curveService.getYAxisValue(labcode, primaryKey, sensorNo, xAxisValue);
    }
    //查询所有名牌信息条目
    //http://localhost/consumer/CurveInfoAPI/listTestProdInfoItems
    @CrossOrigin
    @RequestMapping(value = "/listTestProdInfoItems",method = RequestMethod.GET)
    public List<TestProdInfoItemModel> listTestProdInfoItems(){
        return curveService.listTestProdInfoItems();
    }
    //获取某个台位的测试元数据（铭牌信息）
    //http://localhost/consumer/CurveInfoAPI/getTestProdInfo?labcode=Haierrefrigerator20151214&testunitno=6
    @CrossOrigin
    @RequestMapping(value = "/getTestProdInfo",method=RequestMethod.GET)
    public List<TestProdInfoVo> getTestProdInfo(HttpServletRequest request){
        String labcode = request.getParameter("labcode");
        String testunitno = request.getParameter("testunitno");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        return curveService.getTestProdInfo(labcode,testunitno,pageNum,pageSize);
    }
    /***
     * 传感器设置，在middile_sensorConfigLayer
     * */
    //查询所有的传感器信息
    //http://localhost/consumer/CurveInfoAPI/getAllSensorName
    @CrossOrigin
    @RequestMapping(value = "/getAllSensorName",method=RequestMethod.GET)
    public List<Select2> getAllSensorName(){
        return curveService.getSensorInfoByCondition();
    }
    //查询出台位对应的所有的传感器分组信息 参数primaryKey
    //http://localhost/consumer/CurveInfoAPI/getAllGroupInfoByCondition?primaryKey=2017-11-1611:17:211
    @CrossOrigin
    @RequestMapping(value = "/getAllGroupInfoByCondition",method = RequestMethod.GET)
    public List<GroupInfoModel> getAllGroupInfoByCondition(String primaryKey){
      return curveService.getAllGroupInfoByCondition(primaryKey);
    }
    //根据groupNO 获取 sensorInfo
    //http://localhost/consumer/CurveInfoAPI/getSensorInfoByGroupNo?primaryKey=2017-11-1611:17:211&groupNo=1
    @CrossOrigin
    @RequestMapping(value = "/getSensorInfoByGroupNo",method = RequestMethod.GET)
    public List<SensorInfoModel> getSensorInfoByGroupNo(@RequestParam("primaryKey") String primaryKey,@RequestParam("groupNo") String groupNo){
        return curveService.getSensorInfoByGroupNo(primaryKey,groupNo);
    }
    //查询出所有未分配的传感器
    @CrossOrigin
    @RequestMapping(value = "/listUngroupedSensors",method = RequestMethod.GET)
    public List<SensorInfoModel> listUngroupedSensors(String primaryKey){
        return curveService.listUngroupedSensors(primaryKey);
    }
    //通过primaryKey更新传感器的信息
    @CrossOrigin
    @RequestMapping(value = "/updateSensorsByPrimaryKey",method = RequestMethod.POST)
    public String updateSensorsByPrimaryKey(@RequestBody SensorInfoVo sensorInfoVo){
        return curveService.updateSensorsByPrimaryKey(sensorInfoVo);
    }
    //根据primaryKey和传感器编号获得所有的测试数据
    @CrossOrigin
    @RequestMapping(value = "/getSensorTestDataNow",method = RequestMethod.GET)
    public List<CurveVo> getSensorTestDataNow(String labcode,String primaryKey,String sensorno){
        return curveService.getSensorTestDataNow(labcode, primaryKey, sensorno);
    }
    //根据primaryKey查询被使用的传感器信息
    @CrossOrigin
    @RequestMapping(value = "/getAllSelectSensorName",method = RequestMethod.GET)
    public List<SensorInfoModel> getAllSelectSensorName(String primaryKey){
        return curveService.getAllSelectSensorName(primaryKey);
    }
    //数据列表
    @CrossOrigin
    @RequestMapping(value = "/getAllTestDataVo",method = RequestMethod.GET)
    public PageInfoResult<TestDataVo> getAllTestDataVo(HttpServletRequest request){
        String labcode = request.getParameter("labcode");
        String testunitno = request.getParameter("testunitno");
        String startdate = request.getParameter("startdate");
        String enddate = request.getParameter("enddate");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        return curveService.getAllTestDataVo(labcode,testunitno,startdate,enddate,pageNum,pageSize);
    }
    //http://localhost/consumer/CurveInfoAPI/getStatisticsSensorInfoByCondition?primaryKey=2017-11-1611:17:211&groupNo=1&startTime=0&endTime=6
    @CrossOrigin
    @RequestMapping(value = "/getStatisticsSensorInfoByCondition",method = RequestMethod.GET)
    public List<TestDataStatisticsVo> getStatisticsSensorInfoByCondition(String primaryKey, String groupNo,
                                                                         String startTime, String endTime){
        return curveService.getStatisticsSensorInfoByCondition(primaryKey, groupNo, startTime, endTime);
    }
    @CrossOrigin
    @RequestMapping(value = "/putDivCruveData",method = {RequestMethod.GET, RequestMethod.POST})
    public String putDivCruveData(HttpServletRequest request){
        String primaryKey = request.getParameter("primaryKey");
        String cruveData = request.getParameter("cruveData");
        return curveService.putDivCruveData(primaryKey,cruveData);
    }
    @CrossOrigin
    @RequestMapping(value = "/putCanvasCruveData",method = {RequestMethod.GET, RequestMethod.POST} )
    public String putCanvasCruveData(String primaryKey,String cruveData){
        return curveService.putCanvasCruveData(primaryKey, cruveData);
    }
    @CrossOrigin
    @RequestMapping(value = "/putLeftSensorData",method = {RequestMethod.GET, RequestMethod.POST})
    public String putLeftSensorData(String primaryKey,String cruveData){
        return curveService.putLeftSensorData(primaryKey, cruveData);
    }
    @CrossOrigin
    @RequestMapping(value = "/getAllTestProdInfoValueNow",method = RequestMethod.GET)
    public List<TestProdInfoValueModel> getAllTestProdInfoValueNow(String labcode){
        return curveService.getAllTestProdInfoValueNow(labcode);
    }
    //http://localhost/consumer/CurveInfoAPI/exportTestData?labCode=Haierrefrigerator20151214&primarykey=2017-11-1611:17:211&testUnit=1&startDate=634&endDate=635
    @CrossOrigin
    @RequestMapping(value="/exportTestData",method = RequestMethod.GET)
    public void exportTestData(HttpServletResponse response, String labCode, int testUnit, String startDate, String endDate){
        //int testno = Integer.parseInt(testUnit);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=DataList.xlsx");
        curveService.exportTestData(labCode,testUnit,startDate,endDate);
    }
    @CrossOrigin
    @RequestMapping(value = "/getDivCruveData",method = RequestMethod.GET)
    public String getDivCruveData(String primaryKey){
        return curveService.getDivCruveData(primaryKey);
    }
    @CrossOrigin
    @RequestMapping(value = "/getLeftSensorData",method = RequestMethod.GET)
    public String getLeftSensorData(String primaryKey){
        return curveService.getLeftSensorData(primaryKey);
    }




}
